package gameam.business;

//Base class for test
import java.io.IOException;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
 
public class MCBaseTest
{
	private final static Logger LOGGER = Logger.getLogger(MCBaseTest.class.getName());

	private ArrayList<MCBaseQuestion> _questions;//all questions in the test
	int _questionIndex;//current question the user is on
	int _totalMark;//total mark for the test so far
	
	//default constructor
	public MCBaseTest()
	{
		LOGGER.setLevel(Level.INFO);
		_questions = new ArrayList<MCBaseQuestion>();
		_questionIndex = 0;
		_totalMark = 0;
	}
	
	//overloaded constructor
	public MCBaseTest(ArrayList<MCBaseQuestion> questions)
	{
		_questions = new ArrayList<MCBaseQuestion>(questions);
		_questionIndex = 0;
		_totalMark = 0;
	}
	
	//functionality to add questions to the test
	public void AddQuestion(MCBaseQuestion question)
	{
		if(!_questions.contains(question))
		{
			_questions.add(question);
		}
		else
		{
			LOGGER.severe("AddQuestion: Attempted to add an existing question.");
		}
	}
	
	//functionality to remove questions from the test
	public void RemoveQuestion(MCBaseQuestion question)
	{
		while(_questions.contains(question))
		{
			_questions.remove(question);
		}		
	}
	
	public MCBaseQuestion GetCurrentQuestion()
	{
		return _questions.get(_questionIndex);
	}
	
	//calculates the maximum possible mark for the test (100%)
	public int MaxMarkForTest()
	{
		Iterator questionIterator = _questions.iterator();
		
		int total = 0;
		
		while(questionIterator.hasNext())
		{
			int maxMarkForQuestion = ((MCBaseQuestion)questionIterator.next()).MaxMarkForQuestion();
			total = total + maxMarkForQuestion;
		}
		return total;
	}
	
	//calculates the worst possible mark for the test (all the wrong answers are chosen)
	public int MinMarkForTest()
	{
		Iterator questionIterator = _questions.iterator();
		
		int total = 0;
		
		while(questionIterator.hasNext())
		{
			int minMarkForQuestion = ((MCBaseQuestion)questionIterator.next()).MinMarkForQuestion();
			total = total + minMarkForQuestion;
		}
		return total;
	}
	
	public void ResetTest()
	{
		_totalMark = 0;
		_questionIndex = 0;
	}
	
	//call this on the "NEXT" Button Click
	public int ProceedToNextQuestion()
	{
		_totalMark = _totalMark + _questions.get(_questionIndex).AchievedMarkForQuestion();
		_questionIndex = _questionIndex + 1;
		if (_questionIndex >= _questions.size())
		{
			_questionIndex = _questions.size() - 1; //zero based size
			return _totalMark; //test complete
		}
		
		return -1; //no change
	}
	
/*	//call this on the "PREVIOUS" Button Click
	public void GoToPreviousQuestion()
	{
		_questionIndex = _questionIndex - 1;
		if (_questionIndex < 0)
		{
			_questionIndex = 0;
		}
		else
		{
			_totalMark = _totalMark - _questions.get(_questionIndex).AchievedMarkForQuestion();
		}
	} */
	
	////TODO: add in functionality to select an answer to question - see functionality provided in MCBaseQuestion and MCAnswer
}